import { defineStore } from "pinia"
import { ref } from "vue"
import type * as Football from "@/pages/xinhaozhi/apis/type"
import { 
  toFixedFour, 
  generateHourlyNullSeries, 
  mergeDataWithNullSeries, 
  fillNullsWithPreviousValue, 
  getTimeRange, 
  generateCompanyChartData,
  type ChartData 
} from "./utils"

export const useChartSubStore = defineStore("chartSub", () => {
  // State
  // 图表数据
  const leftBottomChart293 = ref<ChartData>({ xData: [], seriesData: [] })
  const leftBottomChart18 = ref<ChartData>({ xData: [], seriesData: [] })
  const rightCombinedChartHome = ref<ChartData>({ xData: [], seriesData: [] })
  const rightCombinedChartDraw = ref<ChartData>({ xData: [], seriesData: [] })
  const rightCombinedChartAway = ref<ChartData>({ xData: [], seriesData: [] })

  // Actions

  /** 生成单个公司的图表数据并更新状态 */
  function generateCompanyChart(signals: Football.Signal[], isCompany293: boolean = true): ChartData {
    const chartData = generateCompanyChartData(signals, isCompany293 ? "威廉希尔" : "必发")
    
    if (isCompany293) {
      leftBottomChart293.value = chartData
    } else {
      leftBottomChart18.value = chartData
    }

    return chartData
  }

  /** 生成组合图表数据 */
  function generateCombinedChartData(signals293: Football.Signal[], signals18: Football.Signal[]): void {
    // 如果两个公司都没有数据，清空图表
    if (signals293.length === 0 && signals18.length === 0) {
      resetCombinedCharts()
      return
    }

    // 获取两个公司的时间范围
    const timeRange293 = getTimeRange(signals293)
    const timeRange18 = getTimeRange(signals18)
    
    let timeRange: { start: string; end: string }
    
    if (timeRange293 && timeRange18) {
      // 合并时间范围 - 取两个公司的最早和最晚时间
      const allTimes = [
        timeRange293.start,
        timeRange293.end,
        timeRange18.start,
        timeRange18.end
      ]
      const start = allTimes.reduce((min, current) => current < min ? current : min)
      const end = allTimes.reduce((max, current) => current > max ? current : max)
      timeRange = { start, end }
    } else {
      timeRange = timeRange293 || timeRange18!
    }

    // 生成每小时null的时间序列
    const timePoints = generateHourlyNullSeries(timeRange.start, timeRange.end)
    
    // 合并两个公司的数据
    const home293Data = mergeDataWithNullSeries(signals293, timePoints, 'signal_home')
    const home18Data = mergeDataWithNullSeries(signals18, timePoints, 'signal_home')
    const draw293Data = mergeDataWithNullSeries(signals293, timePoints, 'signal_draw')
    const draw18Data = mergeDataWithNullSeries(signals18, timePoints, 'signal_draw')
    const away293Data = mergeDataWithNullSeries(signals293, timePoints, 'signal_away')
    const away18Data = mergeDataWithNullSeries(signals18, timePoints, 'signal_away')

    // 生成图表数据
    rightCombinedChartHome.value = {
      xData: timePoints,
      seriesData: [
        { name: "威廉希尔-主队信号", data: home293Data },
        { name: "必发-主队信号", data: home18Data }
      ]
    }

    rightCombinedChartDraw.value = {
      xData: timePoints,
      seriesData: [
        { name: "威廉希尔-平局信号", data: draw293Data },
        { name: "必发-平局信号", data: draw18Data }
      ]
    }

    rightCombinedChartAway.value = {
      xData: timePoints,
      seriesData: [
        { name: "威廉希尔-客队信号", data: away293Data },
        { name: "必发-客队信号", data: away18Data }
      ]
    }
  }

  /** 重置组合图表数据 */
  function resetCombinedCharts() {
    rightCombinedChartHome.value = { xData: [], seriesData: [] }
    rightCombinedChartDraw.value = { xData: [], seriesData: [] }
    rightCombinedChartAway.value = { xData: [], seriesData: [] }
  }

  /** 重置所有图表数据 */
  function resetAllCharts() {
    leftBottomChart293.value = { xData: [], seriesData: [] }
    leftBottomChart18.value = { xData: [], seriesData: [] }
    resetCombinedCharts()
  }

  return {
    // 状态
    leftBottomChart293,
    leftBottomChart18,
    rightCombinedChartHome,
    rightCombinedChartDraw,
    rightCombinedChartAway,
    
    // 方法
    generateCompanyChart,
    generateCombinedChartData,
    resetCombinedCharts,
    resetAllCharts,
    
    // 工具函数
    toFixedFour,
    generateHourlyNullSeries,
    mergeDataWithNullSeries,
    fillNullsWithPreviousValue,
    getTimeRange,
    generateCompanyChartData
  }
})
